package com.jinniulive.platform.module.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.activity.MainActivity;
import com.jinniulive.platform.bean.RegisterInfo;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.common.util.MD5;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 注册页
 */
public class SubEnrollActivity extends AppCompatActivity implements View.OnFocusChangeListener, TextWatcher {

    private EditText mEditTextPassword,mEditTextPassword2;
    private Button mButtonAgreement,mButtonLogin;
    private TextView mTextViewAgreement;
    private String phoneNumber;
    private Boolean isAgreement =true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_enroll);
        phoneNumber = getIntent().getStringExtra("PhoneNumber");
        initView();
        setlistener();

    }

    private void setlistener() {
        //失去焦点监听
        mEditTextPassword.setOnFocusChangeListener(this);
        mEditTextPassword2.setOnFocusChangeListener(this);
        //文本监听
        mEditTextPassword.addTextChangedListener(this);
        mEditTextPassword2.addTextChangedListener(this);

    }

    private void initView() {
        mEditTextPassword = (EditText) findViewById(R.id.activity_sub_enroll_password);
        mEditTextPassword2 = (EditText) findViewById(R.id.activity_sub_enroll_password2);
        mButtonAgreement = (Button) findViewById(R.id.activity_sub_enroll_bt_agreement);
        mButtonLogin = (Button) findViewById(R.id.activity_sub_enroll_bt_login);
        mTextViewAgreement = (TextView) findViewById(R.id.activity_sub_enroll_tv_xieyi);
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()){
                //返回
                case R.id.activity_sub_enroll_back:
                    finish();
                 break;
                //是否同意协议
                case R.id.activity_sub_enroll_bt_agreement:
                    if (isAgreement){
                        mButtonAgreement.setBackgroundDrawable(getResources().getDrawable(R.mipmap.zc1_03));
                        mButtonLogin.setEnabled(false);
                        mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
                        isAgreement = false;
                    }else{
                        mButtonAgreement.setBackgroundDrawable(getResources().getDrawable(R.mipmap.zc1_04));
                        isAgreement = true;
                        if (!TextUtils.isEmpty(mEditTextPassword2.getText().toString().trim())){
                            mButtonLogin.setEnabled(true);
                            mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_14));
                        }
                    }
                    break;
                //登陆
                case R.id.activity_sub_enroll_bt_login:
                    String password = mEditTextPassword.getText().toString().trim();
                    final String password2 = mEditTextPassword2.getText().toString().trim();
                    if (isLetterOrDigit(password2)){
                        if (password2.length()<6||password2.length()>16){
                            Toast.makeText(SubEnrollActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                        }else {
                            if (password.equals(password2)){
                                final String passwordMd5 = MD5.md5(password2);
                                String format = String.format(NetConfig.ZC, phoneNumber, passwordMd5);
                                RequestParams params = new RequestParams(format);
                                x.http().get(params, new Callback.CommonCallback<String>() {
                                    @Override
                                    public void onSuccess(String result) {
                                        Gson gson=new Gson();
                                        RegisterInfo registerInfo = gson.fromJson(result, RegisterInfo.class);
                                        int errorcode = registerInfo.getErrorcode();
                                        if (errorcode==200){
                                            //自动登录
                                            SharedPreferences sp = getSharedPreferences("autoLogin", MODE_PRIVATE);
                                            SharedPreferences.Editor edit=sp.edit();
                                            edit.putString("account",phoneNumber);
                                            edit.putString("password",password2);
                                            edit.commit();
                                            startActivity(new Intent(SubEnrollActivity.this, MainActivity.class));
                                            finish();
                                        }else{
                                            Toast.makeText(SubEnrollActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onError(Throwable ex, boolean isOnCallback) {

                                    }

                                    @Override
                                    public void onCancelled(CancelledException cex) {

                                    }

                                    @Override
                                    public void onFinished() {

                                    }
                                });

                            }else{
                                Toast.makeText(this, "俩次输入的密码不同请重新输入", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }else{
                        Toast.makeText(SubEnrollActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;

            }
        }
    }
    //判断字符串是否是 数字或字母
    public static boolean isLetterOrDigit(String str) {
        boolean isLetterOrDigit = false;//定义一个boolean值，用来表示是否包含字母或数字
        for (int i = 0; i < str.length(); i++) {
            if (Character.isLetterOrDigit(str.charAt(i))) {   //用char包装类中的判断数字的方法判断每一个字符
                isLetterOrDigit = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isLetterOrDigit && str.matches(regex);
        return isRight;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
       if (hasFocus){
           //得到焦点
       }else{
           //失去
           switch (v.getId()){
               case R.id.activity_sub_enroll_password:
                   String password = mEditTextPassword.getText().toString().trim();
                   if (isLetterOrDigit(password)){
                       if (password.length()<6){
                           Toast.makeText(SubEnrollActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();

                       }
                   }else{
                       Toast.makeText(SubEnrollActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                   }
                   break;
               case R.id.activity_sub_enroll_password2:
                   String password2 = mEditTextPassword2.getText().toString().trim();
                   if (isLetterOrDigit(password2)){
                       if (password2.length()<6){
                           Toast.makeText(SubEnrollActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                       }
                   }else{

                       Toast.makeText(SubEnrollActivity.this, "请输入6-16位数字或字母", Toast.LENGTH_SHORT).show();
                   }
                   break;
           }

       }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(mEditTextPassword.getText().toString().trim())){
            mEditTextPassword2.setEnabled(false);
            mEditTextPassword2.setText("");
        }else{
            mEditTextPassword2.setEnabled(true);
        }
        //判断 设置协议
        if (TextUtils.isEmpty(mEditTextPassword2.getText().toString().trim())) {
            mButtonLogin.setEnabled(false);
            mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
        }else{
            if (!isAgreement){
                mButtonLogin.setEnabled(false);
                mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_12));
            }else{
                mButtonLogin.setEnabled(true);
                mButtonLogin.setBackgroundDrawable(getResources().getDrawable(R.mipmap.xyb1_14));
            }
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
